<template>
  <div class="profile-container">
    <!-- 顶部导航 -->
    <div class="header">
      <div class="header-content">
        <el-button @click="goBack" type="text" class="back-btn">
          <el-icon><ArrowLeft /></el-icon>
          返回
        </el-button>
        <h2>个人信息</h2>
        <div></div>
      </div>
    </div>

    <!-- 个人信息表单 -->
    <div class="profile-form">
      <el-form 
        ref="profileFormRef" 
        :model="profileForm" 
        :rules="profileRules" 
        label-width="100px"
        class="form-container"
      >
        <el-form-item label="头像" prop="avatar">
          <div class="avatar-upload">
            <el-avatar 
              :size="100" 
              :src="profileForm.avatar"
              class="profile-avatar"
            >
              {{ profileForm.name?.charAt(0) || 'U' }}
            </el-avatar>
            <div class="avatar-actions">
              <el-button type="primary" size="small" icon="Upload">
                更换头像
              </el-button>
              <el-button type="info" size="small" icon="Camera" plain>
                拍照
              </el-button>
            </div>
          </div>
        </el-form-item>

        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="profileForm.username" 
            placeholder="请输入用户名"
            disabled
            prefix-icon="User"
          />
          <div class="form-tip">
            <el-icon><InfoFilled /></el-icon>
            用户名不可修改
          </div>
        </el-form-item>

        <el-form-item label="姓名" prop="name">
          <el-input 
            v-model="profileForm.name" 
            placeholder="请输入真实姓名"
            prefix-icon="UserFilled"
            clearable
          />
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input 
            v-model="profileForm.phone" 
            placeholder="请输入手机号"
            prefix-icon="Phone"
            clearable
          />
        </el-form-item>

        <el-form-item label="身份证号" prop="idCard">
          <el-input 
            v-model="profileForm.idCard" 
            placeholder="请输入身份证号"
            prefix-icon="Document"
            clearable
            show-word-limit
            maxlength="18"
          />
        </el-form-item>

        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="profileForm.gender" class="gender-group">
            <el-radio label="male">
              <el-icon><Male /></el-icon>
              男
            </el-radio>
            <el-radio label="female">
              <el-icon><Female /></el-icon>
              女
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker
            v-model="profileForm.birthday"
            type="date"
            placeholder="选择出生日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            prefix-icon="Calendar"
            style="width: 100%"
          />
        </el-form-item>

        <el-form-item label="地址" prop="address">
          <el-input 
            v-model="profileForm.address" 
            type="textarea"
            :rows="3"
            placeholder="请输入详细地址"
            prefix-icon="Location"
            show-word-limit
            maxlength="200"
          />
        </el-form-item>

        <el-form-item>
          <el-button 
            type="primary" 
            size="large" 
            class="save-button"
            :loading="loading"
            @click="saveProfile"
          >
            保存修改
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

export default {
  name: 'Profile',
  setup() {
    const router = useRouter()
    const profileFormRef = ref()
    const loading = ref(false)

    const profileForm = reactive({
      username: '',
      name: '',
      phone: '',
      idCard: '',
      gender: 'male',
      birthday: '',
      address: '',
      avatar: ''
    })

    const profileRules = {
      name: [
        { required: true, message: '请输入姓名', trigger: 'blur' },
        { min: 2, max: 20, message: '姓名长度在2到20个字符', trigger: 'blur' }
      ],
      phone: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
      ],
      idCard: [
        { required: true, message: '请输入身份证号', trigger: 'blur' },
        { pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, message: '请输入正确的身份证号', trigger: 'blur' }
      ],
      gender: [
        { required: true, message: '请选择性别', trigger: 'change' }
      ],
      birthday: [
        { required: true, message: '请选择出生日期', trigger: 'change' }
      ],
      address: [
        { required: true, message: '请输入地址', trigger: 'blur' },
        { min: 5, max: 200, message: '地址长度在5到200个字符', trigger: 'blur' }
      ]
    }

    // 加载用户信息
    const loadUserProfile = () => {
      const userStr = localStorage.getItem('user')
      if (userStr) {
        const user = JSON.parse(userStr)
        Object.assign(profileForm, {
          username: user.username || '',
          name: user.name || '',
          phone: user.phone || '',
          idCard: user.idCard || '',
          gender: user.gender || 'male',
          birthday: user.birthday || '',
          address: user.address || '',
          avatar: user.avatar || ''
        })
      }
    }

    // 保存个人信息
    const saveProfile = async () => {
      try {
        await profileFormRef.value.validate()
        loading.value = true

        // 模拟API调用
        await new Promise(resolve => setTimeout(resolve, 1000))

        // 更新本地存储的用户信息
        const userStr = localStorage.getItem('user')
        if (userStr) {
          const user = JSON.parse(userStr)
          const updatedUser = { ...user, ...profileForm }
          localStorage.setItem('user', JSON.stringify(updatedUser))
        }

        ElMessage.success('个人信息保存成功')
        router.go(-1)
      } catch (error) {
        if (error !== false) {
          ElMessage.error('保存失败，请重试')
        }
      } finally {
        loading.value = false
      }
    }

    // 返回上一页
    const goBack = () => {
      router.go(-1)
    }

    onMounted(() => {
      loadUserProfile()
    })

    return {
      profileForm,
      profileFormRef,
      profileRules,
      loading,
      saveProfile,
      goBack
    }
  }
}
</script>

<style scoped>
.profile-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 15px 0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.back-btn {
  color: white;
  font-size: 16px;
  display: flex;
  align-items: center;
}

.back-btn .el-icon {
  margin-right: 5px;
}

.header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: bold;
}

.profile-form {
  max-width: 600px;
  margin: 30px auto;
  padding: 0 20px;
}

.form-container {
  background: white;
  border-radius: 15px;
  padding: 30px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.avatar-upload {
  display: flex;
  align-items: center;
  gap: 20px;
}

.avatar-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.profile-avatar {
  border: 3px solid #409EFF;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
}

.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 8px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.gender-group {
  display: flex;
  gap: 30px;
}

.gender-group .el-radio {
  display: flex;
  align-items: center;
  gap: 5px;
}

.save-button {
  width: 100%;
  height: 50px;
  font-size: 18px;
  border-radius: 25px;
}

@media (max-width: 768px) {
  .profile-form {
    padding: 0 15px;
  }
  
  .form-container {
    padding: 20px;
  }
}
</style> 